//app.js
var qcloud = require('./vendor/wafer2-client-sdk/index')
var config = require('./config')
var util = require('./utils/util.js');

App({
    onLaunch: function () {
        qcloud.setLoginUrl(config.service.loginUrl)
    },
    globalData: {
      //设备信息
      systemInfo: null,
      //微信用户唯一id
      wxCode: wx.getStorageSync('wxcode') || null,
      openid: wx.getStorageSync('openid') || null,
      unionid: wx.getStorageSync('unionid') || null,
      phone: wx.getStorageSync('phone') || null,
      sessionkey: wx.getStorageSync('sessionkey') || null,
      //微信用户信息
      userInfo: wx.getStorageSync('userInfo') || null,
      //判断用户是否已有简历
      isHaveResume: wx.getStorageSync('isHaveResume') || null,
      //用于职位详情绑定数据
      positionDetail: null,
      //  用户登录状态
      token: true,
      //用于保存推送码
      globalFormIds: wx.getStorageSync("globalFormIds") || null
    },
    api:{
      getJob: "index/getJobByHot",
      getList:"index/serachJobByCandidate",
      addRecommend:"index/Recommend",
      login:"index/wxlogin",
      getwxUserInfo: "index/getwxUserInfo",
      getUserInfo: "index/getUserInfo",
      isExistRec:"index/isExistRecommendself",
      isHaveResume:"index/isHaveResume",
      myrecommend:"index/myrecommend",
      resumeDomain:"index/resumeDomain",
      sendCode:"index/sendCode",
      do_login:"index/bindePhone",
      similarPosi:"index/similarPosi",
      getAdcarouse:"index/getAdcarouse",
      getArticleDetail:"index/getArticleDetail",
      getTopJobByHot:"index/getTopJobByHot",
      getTopHunter: "index/getTopHunter",
      getAdContent:"index/getAdContent",
      getChannel: "index/getChannel",
      getArticle: "index/getArticle",
      getTopArticleByHot:"index/getTopArticleByHot",
      createUserInfo: "index/createUserInfo",
      getHunterList: "new/getHunterList",
      getHunterDetail: "new/hunterdetail",
      getOrderDescList:"new/getOrderDescList",
      getUserCouponList: "new/getUserCoupon",
      getOrderDescId:"new/getOrderDescId",
      createEvaluate:"new/createEvaluate",
      selectByEvaId:"new/selectByEvaId",
      selectEvaByOrderId:"new/selectEvaByOrderId",
      replyEvaluate:"new/replyEvaluate",
      myassess:"new/myassess",
      selectByArticle:"new/selectByArticle",
      getHunterArticles:"new/getHunterArticles",
      getOrderTime:"new/getOrderTime",
      getOrderTimeId:"new/getOrderTimeId",
      getIdByOrdercode:"new/getIdByOrdercode",
      getResume: "user/getResume",
      getjobtitle:"user/getjobtitle",
      getJobByTypes:"user/getJobByTypes",
      getcity:"user/getcity",
      getdomain:"user/getdomain",
      getCompany:"user/getCompany",
      getmeddomain:"user/getmeddomain",
      getdrugdomain:"user/getdrugdomain",
      getSubDomain:"user/getSubDomain",
      getCompanyByDomain:"user/getCompanyByDomain",
      addOpinion:"user/addPosiOpinion",
      empDomain:"user/getCompanyByEmpDomain",
      getFunction:"user/getFunction",
      getPhoneNumber: "user/getPhoneNumber",
      callPhone:"user/callPhone",
      wxpay:"pay/prepay",
      wxpaydiff:"pay/prepaydiff",
      addOrder: "pay/addOrder",
      updOrder:"pay/updOrder",
      getUnOrderByStatus:"pay/getUnOrderByStatus",
      getOrderingByStatus:"pay/getOrderingByStatus",
      getSafeOrderByStatus:"pay/getSafeOrderByStatus",
      getOrderedByStatus:"pay/getOrderedByStatus",
      getAssessOrderByStatus:"pay/getAssessOrderByStatus",
      myPayDetail:"pay/myPayDetail",
      updateOrder:"pay/updateOrder",
      updatePaystatus: "pay/updatePaystatus",
      createSafesales:"pay/createSafesales",
      mysafed:"pay/mysafed",
      getOrderedById:"pay/getOrderedById",
      getSafedById: "pay/getSafedById",
      saveFormIds: "form/saveFormIds",
      isPaySalaryItem:"pay/isPaySalaryItem",
      isHaveSubmitInfo:"orderResume/hasSubmitInfo",
      
      //基本信息
      getCompanys:"common/getCompanys",
      getJobs:"common/getJobs",
      getCitys:"common/getCitys",
      getDomains:"common/getDomains",
      checkPhone:"user/checkPhone",
      getByUnionid:"user/getByUnionid",
      userSave:"user/save"
    },
    alert: function (msg) {
      if(msg==null){
        msg="消息为空";
      }
      wx.showModal({
          content: msg,
          showCancel: false,
      });
    },
    loading: function () {
        wx.showLoading({
            title: '加载中...',
            mask: true
        });
    },
    hideloading: function () {
        wx.hideLoading();
    },
    getOpenId:function(){
      return wx.getStorageSync('openid');
    },
    getWxCode: function () {
      return wx.getStorageSync('wxcode');
    },
    getUserInfo:function(){
      return wx.getStorageSync('userInfo');
    },
    getSessionKey: function () {
      return wx.getStorageSync('sessionkey');
    },
    hasLogin:function(){
      if (this.globalData.openid == null || this.globalData.userInfo == null){
          return false;
      }
      return true;
    },

  login: function (callback) {
    var that = this;
    wx.login({
      success: function (res) {
        console.log(res);
        if (res.code) {
          var wxcode = res.code;
          wx.getUserInfo({
            success: function (res) {
              util.post(that.api.getwxUserInfo, { code: wxcode, iv: res.iv, encryptedData: res.encryptedData }, function (res) {
                if (res.code != "200") {
                  console.log("微信授权失败");
                } else {
                  console.log(res);
                  var openId = res.data.openId;
                  var unionid = res.data.unionId;
                  //到这一步所有需要的信息已经获取，在这里同时把openid和userinfo存到Storage中          
                  that.globalData.wxCode = wxcode;
                  that.globalData.userInfo = res.data;
                  that.globalData.openid = openId;
                  that.globalData.unionid = unionid;
                  wx.setStorageSync('userInfo', res.data);
                  wx.setStorageSync('openid', openId);
                  wx.setStorageSync('unionid', unionid);
                  wx.setStorageSync('wxcode', wxcode);
                  callback(openId, unionid, res.data, wxcode);
                }
              });
            },
            fail: function (res) {
              console.log("授权失败");
            }
          });
        }
      },
      fail: function (res) {
        console.log("登录失败");
      }
    });
  }

           
})